var config = require("../../config");
var Message = require("../models/message");
var laypage = require("laypage")
var async = require('async');


//留言列表
exports.list = function (req, res) {
	var pageSize = 20; //每页显示条数
	var page = req.params.num - 1 || 0;
	Message.find({type:1}).count().exec(function (err, sum) {
		Message.find({type:1}).limit(pageSize).skip(pageSize * page).sort({_id: -1}).exec(function (err, _message) {
			res.render('admin/messageList', {
				title: '留言列表',
				message: _message,
				laypage: laypage({
					curr: req.params.num || 1,
					url: req.url, //必传参数，获取当前页的url
					groups: 5,
					pages: Math.ceil(sum/pageSize), //分页总数你需要通过sql查询得到
					prev: '‹',
					next: '›'
				}),
				pagesize: sum
			})
		});
	});
}
//留言列表
exports.list2 = function (req, res) {
    var pageSize = 20; //每页显示条数
    var page = req.params.num - 1 || 0;
    Message.find({type:2}).count().exec(function (err, sum) {
        Message.find({type:2}).limit(pageSize).skip(pageSize * page).sort({_id: -1}).exec(function (err, _message) {
			res.render('admin/registrationList', {
				title: '报名列表',
				message: _message,
				laypage: laypage({
					curr: req.params.num || 1,
					url: req.url, //必传参数，获取当前页的url
					groups: 5,
					pages: Math.ceil(sum/pageSize), //分页总数你需要通过sql查询得到
					prev: '‹',
					next: '›'
				}),
				pagesize: sum
			})
        });
    });
}
//留言详情
exports.details = function (req, res) {
	var id = req.params.id;
	Message.findOne({"_id": id}, function (err, _message) {
		res.render("admin/messageDetails", {
			message: _message,
			title: "留言详情"
		});
	});
}
//留言提交
exports.save = function (req, res) {
	var Messages = {};
	if(req.body.type == 2){
        Messages.catName = req.body.catName
        Messages.phone = req.body.phone
        Messages.username = req.body.username
        Messages.typeinfo = req.body.typeinfo
        Messages.type = req.body.type
	}
    if(req.body.type == 1){
        Messages.phone = req.body.phone
        Messages.username = req.body.username
        Messages.qq = req.body.qq
        Messages.content = req.body.content
        Messages.type = req.body.type
	}
	Message.create(Messages, function (err,Message) {
		if(err){
			res.send({"success":false,"msg":"留言失败"})
		}else{
			res.send({"success":true,"msg":"留言成功"})
		}

	});
}

//删除留言
exports.delete = function (req, res) {
	var id = req.params.id;
	Message.remove({
		_id: id
	}, function (err, cases) {
		res.redirect("/admin/messageList");
	})
}
